#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    string addBinary(string a, string b) {
        string str;
        int na = a.size() - 1;
        int nb = b.size() - 1;
        int flag = 0;
        while (na >= 0 && nb >= 0) {
            int x = a[na--] - '0' + b[nb--] - '0' + flag;
            flag = 0;
            if (x >= 2) {
                x -= 2;
                flag = 1;
            }
            str += to_string(x);
        }

        while (nb >= 0) {
            int x = b[nb--] - '0' + flag;
            flag = 0;
            if (x >= 2) {
                x -= 2;
                flag = 1;
            }
            str += to_string(x);
        }
        while (na >= 0) {
            int x = a[na--] - '0' + flag;
            flag = 0;
            if (x >= 2) {
                x -= 2;
                flag = 1;
            }
            str += to_string(x);
        }
        if (flag)
            str += to_string(flag);
        reverse(str.begin(), str.end());
        return str;
    }
};